package com.vimi8.ebb.util;

import com.vimi8.ebb.dto.RegionManamentDto;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by think on 2016/7/24.
 */
public class CommonUtil {
    private static final String DEFAULT_DECIMAL_FORMAT="#.00";
    public static String parseDecimal(Double d){
        java.text.DecimalFormat   df   =new   java.text.DecimalFormat(DEFAULT_DECIMAL_FORMAT);
        String result=  "0.00";
        if(d!=0.00){
            result=df.format(d);
        }
        return result;
    }
    //时间字符串转化为时间类型
    public static Date parseDateStr(String dateStr){
        try {
            return  new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    //把地址代码 11 01 01 001 006 分解成‘省’、‘市’、‘区/县’、‘乡’、‘村’
    public static Map<String,String> parseGeoCodings(String geoCodingsStr){
        Map<String,String> map=new HashMap<String,String>();
       String prov= (geoCodingsStr.substring(0,2)+"0000000000");
        map.put("prov",prov);
        String city= (geoCodingsStr.substring(0,4)+"00000000");
        map.put("city",city);
        String county= (geoCodingsStr.substring(0,6)+"000000");
        map.put("county",county);
        String vilage= (geoCodingsStr.substring(0,9)+"000");
        map.put("vilage",vilage);
        map.put("burg",geoCodingsStr);
        return map;
    }
    //传一个id返回每个字段的值
    public static Map<String,String> parseGeoCodingsField(String geoCodingsStr){
        Map<String,String> map=new HashMap<String,String>();
        String prov= (geoCodingsStr.substring(0,2));
        map.put("prov",(prov));
        String city= (geoCodingsStr.substring(2,4));
        map.put("city",(city));
        String county= (geoCodingsStr.substring(4,6));
        map.put("county",(county));
        String vilage= (geoCodingsStr.substring(6,9));
        map.put("vilage",(vilage));
        String burg=(geoCodingsStr.substring(9,12));
        map.put("burg",(burg));
        return map;
    }
    //判断一个codeId的形状区划 减少与数据的交互
    public static int caculateRegionLever(String codeId){
        int reginLever=0;
        if(null==codeId||"".equals(codeId))
            return reginLever;
        if(codeId.length()>12)return reginLever;
        int burgLever=Integer.valueOf(codeId.substring(9,12));
        if(burgLever>0)return 5;
        int villageLever=Integer.valueOf(codeId.substring(6,9));
        if(villageLever>0)return 4;
        int countyLever=Integer.valueOf(codeId.substring(4,6));
        if(countyLever>0)return 3;
        int cityLever=Integer.valueOf(codeId.substring(2,4));
        if(cityLever>0)return 2;
        int provLever=Integer.valueOf(codeId.substring(0,2));
        if(provLever>0)return 1;
        return reginLever;
    }
    public static String getDecimalPoorRate(int poorPersonTotal,int personTotal){
        double poorRate=(poorPersonTotal*1.0/personTotal)*100;
        String pooRateStr= new DecimalFormat("#.00").format(poorRate)+"%";
        return pooRateStr;
    }
 // public static void main(String[] args){
//       boolean ism= isMobileNO("15628048759");
//        System.out.print("is====="+ism);
//        Message message=new Message();
//        message.setReceiverId("receiverId1,receiverId2");
//        message.setId("sdf");
//        message.setMsgTime(new Date());
//        message.setOwnerId("ownerID");
//        message.setPushMessageId("pushId");
//        String[] receiverIds=null;
//        if(!StringUtils.isEmpty(message.getReceiverId())){
//            receiverIds=message.getReceiverId().split(",");
//        }
//        for(String receiverId:receiverIds){
//            try {
//                Message message1 = (Message) message.clone();
//                message1.setReceiverId(receiverId);
//              System.out.print(message1.toString());
//            } catch (CloneNotSupportedException e) {
//                e.printStackTrace();
//            }
//        }
//        ResourcePoverty resourcePoverty=new ResourcePoverty();
//        resourcePoverty.setResourceType("1");
//      boolean re=  "1"==(resourcePoverty.getResourceType());
//        System.out.print("result==="+re);
  //     String  phnone="13385568559";
   //    boolean isP=isMobileNO(phnone);
    //   System.out.print(isP);

//       Date date = null;
//       try {
//           date = new SimpleDateFormat("yyyy-MM").parse("2017-03");
//           int year=1900+date.getYear();
//            System.out.print("year="+year+",month="+date.getMonth());
//       } catch (ParseException e) {
//
//       }
     //  String phone="0856-6922334";
//       phone=  phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
//      System.out.print("tuoming phone "+phone);
//
//       System.out.print("is mobile "+ isMobileNO(""));

//       if(!StringUtils.isEmpty(phone)&&CommonUtil.isLandlineNO(phone.trim())&&phone.trim().length()==12){
//
//           phone=  phone.replaceAll("(\\d{4})-\\w{4}(\\d{3})","$1*****$2");
//           System.out.print("is mobile "+ phone);
//       }
     //  String fileName="58D1.jpg";
//String suffix="";
    //if(!StringUtils.isEmpty(fileName)&&fileName.contains(".")){
     //   System.out.print("length="+fileName.indexOf("."));
      //  suffix=fileName.substring(fileName.indexOf(".")+1,fileName.length());
      //  System.out.print("suffix="+suffix);
    //}
      //String str="2017-02";
//System.out.print("rr="+str.substring(str.lastIndexOf("-")+1,str.length()));
//String[] regionManamentDtoCollection=new String[]{"t","tt","ttt"};
//      String[] regionManamentDtos=new String[]{"t","tt","ttt","tttt"};
//      for(String regionManamentDto:regionManamentDtoCollection) {
//          for (String regionManamentDto1 : regionManamentDtos) {
//              if (regionManamentDto != null && regionManamentDto.equals(regionManamentDto1))
//                 System.out.print("regionManamentDto1="+regionManamentDto);
//              continue;
//          }
//      }
//  }

    public static boolean isMobileNO(String mobiles){

        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

        Matcher m = p.matcher(mobiles);

      //  System.out.println(m.matches()+"---");

        return m.matches();

        }

    public static boolean isLandlineNO(String mobiles) {
        Pattern p = Pattern.compile("^0\\d{2}-\\d{8}|0\\d{2}-\\d{7}|0\\d{3}-\\d{7}|0\\d{3}-\\d{8}$");
        //  0\d{2}-\d{8}|0\d{2}-\d{7}|0\d{3}-\d{7}|0\d{3}-\d{8}
        Matcher m = p.matcher(mobiles);
        System.out.println(m.matches() + "---");
        return m.matches();
    }


}
